package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.wclass.entity.BaseCollect;
import com.wclass.entity.CourseInfo;
import com.wclass.entity.PostsInfo;
import com.wclass.entity.PostsLike;
import com.wclass.mapper.BaseCollectMapper;
import com.wclass.mapper.PostsInfoMapper;
import com.wclass.service.IBaseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 收藏 服务实现类
 * </p>
 *
 * @author SEN
 * @since 2018-12-26
 */
@Service
public class BaseCollectServiceImpl extends ServiceImpl<BaseCollectMapper, BaseCollect> implements IBaseCollectService {

    @Autowired
    private PostsInfoMapper postsInfoMapper;

    @Override
    public boolean insert(BaseCollect entity) {
        if (entity.getType() == 1 && entity.getMoldId() != null) {
            //帖子收藏数量更新
            PostsInfo postsInfo = new PostsInfo();
            postsInfo.setCollectNum(baseMapper.selectCount(new EntityWrapper<BaseCollect>().eq("mold_id",
                    entity.getMoldId()).eq("type", 1)) + 1);
            postsInfo.setId(entity.getMoldId());
            postsInfoMapper.updateById(postsInfo);
        }
        return super.insert(entity);
    }

    @Override
    public boolean delete(Wrapper<BaseCollect> wrapper) {
        Object type = wrapper.getParamNameValuePairs().get("type");
        Object mold_id = wrapper.getParamNameValuePairs().get("mold_id");

        if (type != null && (int) type == 1 && mold_id != null) {
            //帖子收藏数量更新
            PostsInfo postsInfo = new PostsInfo();
            postsInfo.setCollectNum(baseMapper.selectCount(new EntityWrapper<BaseCollect>().eq("mold_id",
                    wrapper.getParamNameValuePairs().get("mold_id")).eq("type", 1)) + 1);
            postsInfo.setId((long) wrapper.getParamNameValuePairs().get("mold_id"));
            postsInfoMapper.updateById(postsInfo);
        }
        return super.delete(wrapper);
    }
}
